//
//  UIBarButtonItem+ZXExtension.m
//  ZXOCWeiBo
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "UIBarButtonItem+ZXExtension.h"

@implementation UIBarButtonItem (ZXExtension)
/**
 *  @param nmlImg 正常状态的图片
 *  @param hltImg 高亮状态的图片
 *  @param target 按钮taget
 *  @param action 按钮的点击触摸方法
 *
 *  @return UIBarButtonItem
 */
+ (instancetype)barBtnItemWithNmlImg:(NSString *)nmlImg hltImg:(NSString *)hltImg target:(nullable id)target action:(SEL)action
{
   
        //创建一个按钮类型是自定义类型UIButtonTypeCustom
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage * nmlImage = [UIImage imageNamed:nmlImg];
        //设置按钮的属性值
        [btn setImage:nmlImage forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:hltImg] forState:UIControlStateHighlighted];
    btn.bounds = CGRectMake(0, 0, nmlImage.size.width, nmlImage.size.height);
    
        //添加按钮的点击事件
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    
        //创建一个UIBarButtonItem类型的按钮，但是按钮需要设置高亮和普通两种形式，所以要自定义个View在来创建一个but
        UIBarButtonItem * friendsearchBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
    return friendsearchBtn;
}
/**
 *  导航栏的样式
 *
 *  @param title  显示文字
 *  @param target 按钮taget
 *  @param action 按钮的点击触摸方法
 *
 *  @return UIBarButtonItem
 */
+ (instancetype)barBtnItemWithTitl:(NSString *)title target:(id)target action:(SEL)action
{
  
    UIFont * titleFont = [UIFont systemFontOfSize:16];
    //计算文字的尺寸
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize titleSize = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titleFont} context:nil].size;
    
    UIButton * but = [UIButton buttonWithType:UIButtonTypeSystem];
    [but setTitle:title forState:UIControlStateNormal];
    [but setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    //设置不可用字体颜色
    [but setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    //设置按钮文字的大小
    but.titleLabel.font = titleFont;
    but.bounds = CGRectMake(0, 0, titleSize.width, titleSize.height);
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    
    //添加按钮的点击事件
    [but addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return item;
    
}

+(instancetype)backItemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    //按钮普通状态下的背景
    UIImage * nmlImg = [UIImage imageNamed:@"navigationbar_back_withtext"];
    
    NSString * backTitle =title;
    //设置返回按钮
    //设置自定义按钮
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [backBtn setImage:nmlImg forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"navigationbar_back_withtext_highlighted"] forState:UIControlStateHighlighted];
    [backBtn setTitle:backTitle forState:UIControlStateNormal];
    
    //设置按钮的颜色
    [backBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    //设置字体大小
    UIFont * titleFont = [UIFont systemFontOfSize:16];
    backBtn.titleLabel.font = titleFont;
    //计算文字的尺寸
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize titleSize = [backTitle boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titleFont} context:nil].size;
    
    CGFloat btnH = nmlImg.size.height;
    CGFloat btnW = nmlImg.size.width + titleSize.width;
    
    //设置按钮的尺寸
    backBtn.bounds = CGRectMake(0, 0, btnW, btnH);
    //设置导航栏左边的按钮
    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    //添加按钮的点击事件
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return item;
}
@end
